<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item  v-if="isAuth('hotel:hotelliquidation:save')">
        <el-select v-model="dataForm.type" placeholder="类型">
          <el-option label="商家" value="0"></el-option>
          <el-option label="服务商" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.sellerName" placeholder="名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          :clearable="true"
          value-format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item  >
        <el-select v-model="dataForm.finish" placeholder="状态">
          <el-option label="全部" value></el-option>
          <el-option label="未出账" value="0"></el-option>
          <el-option label="已出账" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('Common.Query') }}</el-button>
        <el-button
          v-if="isAuth('hotel:hotelliquidation:save')"
          type="primary"
          @click="liquidationSettingHandler()"
        >结算设置</el-button>
        <el-button
          v-if="isAuth('hotel:hotelliquidation:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >{{ $t('Common.BulkDelete') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border=""
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="period" header-align="center" align="center" label="周期"></el-table-column>
      <el-table-column prop="sellerName" header-align="center" align="center" label="商家名称"></el-table-column>
      <el-table-column prop="orderAmount" header-align="center" align="center" label="订单价格"></el-table-column>
      <el-table-column prop="commission" header-align="center" align="center" label="佣金"></el-table-column>
      <el-table-column prop="amount" header-align="center" align="center" label="结算金额"></el-table-column>
      <el-table-column prop="finishTime" header-align="center" align="center" label="结算时间"></el-table-column>
      <el-table-column prop="finish" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
        <el-tag v-if="scope.row.finish === false" size="small">未出账</el-tag>
        <el-tag v-if="scope.row.finish === true" size="small">已出账</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="exportExcel(scope.row.id,scope.row.period)" >下载</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)" >明细</el-button>
        <!--  <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <view-order v-if="viewOrderVisible" ref="viewOrder" ></view-order>
    <LiquidationSetting v-if="liquidationSettingVisible" ref="liquidationSetting"></LiquidationSetting>
  </div>
</template>

<script>
import AddOrUpdate from "./hotelliquidationrecord";
import ViewOrder from "../order/hotelorder-add-or-update";
import LiquidationSetting from "./hotelliquidationsetting-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        type: "0",
        sellerName: null
      },
      dateRange:[],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      liquidationSettingVisible: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      viewOrderVisible: false
    };
  },
  components: {
    AddOrUpdate,
    LiquidationSetting,
    ViewOrder
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let startTime = null;
      let endTime = null;
      if (this.dateRange.length > 0) {
        startTime = this.dateRange[0];
      }
      if (this.dateRange.length > 1) {
        endTime = this.dateRange[1];
      }
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelliquidation/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          type: this.dataForm.type,
          finish: this.dataForm.finish,
          name: this.dataForm.sellerName,
          startDate: startTime,
          endDate: endTime
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    liquidationSettingHandler() {
      this.liquidationSettingVisible = true;
      this.$nextTick(() => {
        this.$refs.liquidationSetting.init();
      });
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, this.viewOrder);
      });
    },
    viewOrder(id) {
      this.viewOrderVisible = true;
      this.$nextTick(() => {
        this.$refs.viewOrder.init(id);
      });
    },

    exportExcel(id, finishTime) {
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelliquidationrecord/export"),
        method: "post",
        params: this.$http.adornParams({
          liquidationId: id
        }),
        responseType: 'blob'
      }).then(res => this.downloads(res, finishTime))
    },
    downloads (data, finishTime) {
      if (!data) {
        return
      }
      let url = window.URL.createObjectURL(data.data)
      let link = document.createElement('a')
      link.style.display ='none'
      link.href = url
      link.setAttribute('download', `${finishTime}.xlsx`)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      window.URL.revokeObjectURL(url)
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/hotel/hotelliquidation/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
